api.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. """
  3. requests.api
  4. ~~~~~~~~~~~~
  5. This module implements the Requests API.
  6. :copyright: (c) 2012 by Kenneth Reitz.
  7. :license: Apache2, see LICENSE for more details.
  8. """
  9. from . import sessions
  10. def request(method, url, **kwargs):
  11. """Constructs and sends a :class:`Request <Request>`.
  12. :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
  13. :param url: URL for the new :class:`Request` object.
  14. :param params: (optional) Dictionary, list of tuples or bytes to send
  15. in the query string for the :class:`Request`.
  16. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  17. object to send in the body of the :class:`Request`.
  18. :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
  19. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
  20. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
  21. :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
  22. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
  23. or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
  24. defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
  25. to add for the file.
  26. :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
  27. :param timeout: (optional) How many seconds to wait for the server to send data
  28. before giving up, as a float, or a :ref:`(connect timeout, read
  29. timeout) <timeouts>` tuple.
  30. :type timeout: float or tuple
  31. :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
  32. :type allow_redirects: bool
  33. :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
  34. :param verify: (optional) Either a boolean, in which case it controls whether we verify
  35. the server's TLS certificate, or a string, in which case it must be a path
  36. to a CA bundle to use. Defaults to ``True``.
  37. :param stream: (optional) if ``False``, the response content will be immediately downloaded.
  38. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
  39. :return: :class:`Response <Response>` object
  40. :rtype: requests.Response
  41. Usage::
  42. >>> import requests
  43. >>> req = requests.request('GET', 'https://httpbin.org/get')
  44. >>> req
  45. <Response [200]>
  46. """
  47. # By using the 'with' statement we are sure the session is closed, thus we
  48. # avoid leaving sockets open which can trigger a ResourceWarning in some
  49. # cases, and look like a memory leak in others.
  50. with sessions.Session() as session:
  51. return session.request(method=method, url=url, **kwargs)
  52. def get(url, params=None, **kwargs):
  53. r"""Sends a GET request.
  54. :param url: URL for the new :class:`Request` object.
  55. :param params: (optional) Dictionary, list of tuples or bytes to send
  56. in the query string for the :class:`Request`.
  57. :param \*\*kwargs: Optional arguments that ``request`` takes.
  58. :return: :class:`Response <Response>` object
  59. :rtype: requests.Response
  60. """
  61. kwargs.setdefault('allow_redirects', True)
  62. return request('get', url, params=params, **kwargs)
  63. def options(url, **kwargs):
  64. r"""Sends an OPTIONS request.
  65. :param url: URL for the new :class:`Request` object.
  66. :param \*\*kwargs: Optional arguments that ``request`` takes.
  67. :return: :class:`Response <Response>` object
  68. :rtype: requests.Response
  69. """
  70. kwargs.setdefault('allow_redirects', True)
  71. return request('options', url, **kwargs)
  72. def head(url, **kwargs):
  73. r"""Sends a HEAD request.
  74. :param url: URL for the new :class:`Request` object.
  75. :param \*\*kwargs: Optional arguments that ``request`` takes. If
  76. `allow_redirects` is not provided, it will be set to `False` (as
  77. opposed to the default :meth:`request` behavior).
  78. :return: :class:`Response <Response>` object
  79. :rtype: requests.Response
  80. """
  81. kwargs.setdefault('allow_redirects', False)
  82. return request('head', url, **kwargs)
  83. def post(url, data=None, json=None, **kwargs):
  84. r"""Sends a POST request.
  85. :param url: URL for the new :class:`Request` object.
  86. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  87. object to send in the body of the :class:`Request`.
  88. :param json: (optional) json data to send in the body of the :class:`Request`.
  89. :param \*\*kwargs: Optional arguments that ``request`` takes.
  90. :return: :class:`Response <Response>` object
  91. :rtype: requests.Response
  92. """
  93. return request('post', url, data=data, json=json, **kwargs)
  94. def put(url, data=None, **kwargs):
  95. r"""Sends a PUT request.
  96. :param url: URL for the new :class:`Request` object.
  97. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  98. object to send in the body of the :class:`Request`.
  99. :param json: (optional) json data to send in the body of the :class:`Request`.
  100. :param \*\*kwargs: Optional arguments that ``request`` takes.
  101. :return: :class:`Response <Response>` object
  102. :rtype: requests.Response
  103. """
  104. return request('put', url, data=data, **kwargs)
  105. def patch(url, data=None, **kwargs):
  106. r"""Sends a PATCH request.
  107. :param url: URL for the new :class:`Request` object.
  108. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  109. object to send in the body of the :class:`Request`.
  110. :param json: (optional) json data to send in the body of the :class:`Request`.
  111. :param \*\*kwargs: Optional arguments that ``request`` takes.
  112. :return: :class:`Response <Response>` object
  113. :rtype: requests.Response
  114. """
  115. return request('patch', url, data=data, **kwargs)
  116. def delete(url, **kwargs):
  117. r"""Sends a DELETE request.
  118. :param url: URL for the new :class:`Request` object.
  119. :param \*\*kwargs: Optional arguments that ``request`` takes.
  120. :return: :class:`Response <Response>` object
  121. :rtype: requests.Response
  122. """
  123. return request('delete', url, **kwargs)